package cli

import (
	"fmt"

	"gitee.com/smalldeng/cloud-station-g7/store"
	"gitee.com/smalldeng/cloud-station-g7/store/aliyun"
	"gitee.com/smalldeng/cloud-station-g7/store/aws"
	"gitee.com/smalldeng/cloud-station-g7/store/tx"
	"github.com/spf13/cobra"
)

var (
	ossProvier   string
	ossEndpoint  string
	accessKey    string
	accessSecret string
	bucketName   string
	uploadFile   string
)

var UploadCmd = &cobra.Command{
	Use:     "upload",
	Long:    "upload 文件上传",
	Short:   "upload 文件上传",
	Example: "upload -f filename",
	RunE: func(cmd *cobra.Command, args []string) error {
		var (
			uploader store.Uploader
			err      error
		)
		switch ossProvier {
		case "aliyun":
			uploader, err = aliyun.NewAliOssStore(&aliyun.Options{
				Endpoint:     ossEndpoint,
				AccessKey:    accessKey,
				AccessSecret: accessSecret,
			})
		case "tx":
			uploader = tx.NewTxOssStore()
		case "aws":
			uploader = aws.NewAwsOssStore()
		default:
			return fmt.Errorf("not support oss provider %s", ossProvier)

		}
		if err != nil {
			return err
		}
		uploader.Upload(bucketName, uploadFile, uploadFile)
		return nil
	},
}

func init() {
	f := UploadCmd.PersistentFlags()
	f.StringVarP(&ossProvier, "provider", "p", "aliyun", "oss provider[aliyun/tx/aws]")
	f.StringVarP(&ossEndpoint, "endpoint", "e", "oss-cn-hangzhou.aliyuncs.com", "oss aliyun endpoint")
	f.StringVarP(&accessKey, "bucket_name", "b", "hpoi-backup", "oss aliyun bucket name")
	f.StringVarP(&accessKey, "access_key", "k", "", "oss aliyun access key")
	f.StringVarP(&accessSecret, "access_secret", "s", "", "oss aliyun access secret")
	f.StringVarP(&uploadFile, "upload_file", "f", "", "upload file")

	RootCmd.AddCommand(UploadCmd)

}
